
class DdzProxy extends ResourceProxyBase {
    public static NAME: string = "DdzProxy";//必须和excel导出文件一致
    public static Proxy: DdzProxy = null;
    public constructor() {
        super(DdzProxy.NAME);
    }
    public static getProxy() {
        if (!DdzProxy.Proxy) {
            DdzProxy.Proxy = new DdzProxy();
        }
        return DdzProxy.Proxy;
    }
    //登陆成功
    public login(data: Object) {
        DdzVO.VO = null;
        Poker_DDZ.init();
        DdzVO.getVo().lUserScore = GameVO.getVo().lUserScore;
    }
    public startGameDate() {//开始游戏
        let obj = new Object();
        obj["wVisit"] = 0
        obj["wVersion"] = 101122049
        obj["wGameVersion"] = 101122049;
        SocketManager.sendMessage(InstructionsEnum.GAME_TYPE_NAME.DDZ, RegDataDdz.CMD_GP_StartGame, obj)
    }
    //获取大厅配置信息，获取完成后发送匹配玩家请求
    public getHallInfo(data: Object) {
        DdzVO.getVo().dwServerRule = data["dwServerRule"];
        DdzVO.getVo().wChairCount = data["wChairCount"];
        DdzVO.getVo().wServerType = data["wServerType"];
        DdzVO.getVo().wTableCount = data["wTableCount"];
    }
    public getRoomInfo(data: Object) {
        var TableStatusArray: Array<any> = data["TableStatusArray"]
        DdzVO.getVo().lCellScore = TableStatusArray[0]["lCellScore"];
    }

    //用户进入广场 携带用户所有数据 3-100
    public userJoinSquare(data: Object) {
        let user = data["tagMobileUserInfoHead"]
        DdzVO.getVo().userInfo[user.dwUserID] = user;//广场所有人存入集合
        this.userJoinRoom();
    }
    //用户战绩信息刷新 包括金币 3-101
    public getUserInfo(data: Object) {
        let UserScore = data["UserScore"];
        let dwUserID = data["dwUserID"];
        let lScore = UserScore["lScore"];
        var user = DdzVO.getVo().userInfo[dwUserID];
        if (user) {
            user.lScore = lScore;
        }
        this.userJoinRoom();
    }
    //用户状态变更 入座，离开 3-102
    public userStateModify(data: Object) {
        //用户状态发生改变从缓存的所有用户中找到该用户改变其座位
        let dwUserID = data["dwUserID"];
        let UserStatus = data["UserStatus"];
        let wTableID = UserStatus["wTableID"];
        let wChairID = UserStatus["wChairID"];
        let cbUserStatus = UserStatus["cbUserStatus"];
        let user = DdzVO.getVo().userInfo[dwUserID]
        if (user.dwUserID == dwUserID) {
            user.wTableID = wTableID;
            user.wChairID = wChairID;
            user.cbUserStatus = cbUserStatus;
        }
        this.userJoinRoom();
    }

    public userJoinRoom() {
        DdzVO.getVo().initUserInof();
        //状态改变完毕后从所有玩家中找自己是否已入座，若自己入座，则找是否跟自己同桌的用户，将其显示到桌子对面
        var user_my = DdzVO.getVo().userInfo[GameVO.getVo().dwUserID]
        if (user_my) {
            DdzVO.getVo().wTableId_my = user_my.wTableID;
            DdzVO.getVo().wChairId_my = user_my.wChairID;
            DdzVO.getVo().status_my = user_my.cbUserStatus;
            DdzVO.getVo().lUserScore = user_my.lScore
        }
        //我入座找对手
        if (Global.isNotEmpty(DdzVO.getVo().wTableId_my) && DdzVO.getVo().wTableId_my != 65535) {
            DdzVO.getVo().wTableId_my_up = DdzVO.getVo().wTableId_my
            //根据我所在椅子确定其他玩家在桌面坐哪个位置
            let user_R = (DdzVO.getVo().wChairId_my + 1) % 3
            let user_L = (DdzVO.getVo().wChairId_my + 2) % 3
            for (let i in DdzVO.getVo().userInfo) {
                var user = DdzVO.getVo().userInfo[i]
                if (user.wTableID == DdzVO.getVo().wTableId_my &&
                    user.dwUserID != GameVO.getVo().dwUserID) {//跟我坐一桌的就是对手
                    if (user.wChairID == user_R) {
                        DdzVO.getVo().id_right = user.dwUserID
                        DdzVO.getVo().wChairId_right = user.wChairID
                        DdzVO.getVo().faceId_right = user.wFaceID
                        DdzVO.getVo().status_right = user.cbUserStatus
                        DdzVO.getVo().gold_right = user.lScore
                    }
                    if (user.wChairID == user_L) {
                        DdzVO.getVo().id_left = user.dwUserID;
                        DdzVO.getVo().wChairId_left = user.wChairID;
                        DdzVO.getVo().faceId_left = user.wFaceID;
                        DdzVO.getVo().status_left = user.cbUserStatus;
                        DdzVO.getVo().gold_left = user.lScore;
                    }
                }
            }
            game.AppFacade.instance.sendNotification(PanelNotify.DDZ.JOIN_ROOM);
        } else {
            if (DdzVO.getVo().wTableId_my_up != null && DdzVO.getVo().wTableId_my == 65535) {
                //上次有座位此次没座位说明被踢了
                SocketManager.closeSocket(InstructionsEnum.GAME_TYPE_NAME.DDZ);
            }
        }
    }

    //获取游戏进行状态
    public getGameStatus(data: Object) {
        DdzVO.getVo().cbGameStatus = data["cbGameStatus"];
    }
    //游戏开始，发牌
    public startGame(data: Object) {
        DdzVO.getVo().initData();
        game.AppFacade.getInstance().sendNotification(PanelNotify.DDZ.CLEAR_PLAY_GAME);

        DdzVO.getVo().wCurrentUser = data["wCurrentUser"];
        DdzVO.getVo().szUserArea = data["szUserArea"];
        DdzVO.getVo().cbCardData = data["cbCardData"];
        DdzVO.getVo().game_state = 1;
        //每次发牌重置记牌器
        DdzVO.getVo().chessListWhole = { 1: 4, 2: 4, 3: 4, 4: 4, 5: 4, 6: 4, 7: 4, 8: 4, 9: 4, 10: 4, 11: 4, 12: 4, 13: 4, 14: 1, 15: 1 }

        DdzVO.getVo().chessListMy = Poker_DDZ.idChangeObject(DdzVO.getVo().cbCardData);
        DdzVO.getVo().chessListMy = Poker_DDZ.sort(DdzVO.getVo().chessListMy);
        game.AppFacade.instance.sendNotification(PanelNotify.DDZ.START_GAME);
        //开始游戏后就可以叫分
        game.AppFacade.instance.sendNotification(PanelNotify.DDZ.START_CALL_SCORE);
    }
    //进入叫分流程
    // public startCallScore(data: Object) {

    // }
    //单个用户叫分
    public userCallScore(data: Object) {
        DdzVO.getVo().wCallScoreUser = data["wCallScoreUser"];
        DdzVO.getVo().cbUserCallScore = data["cbUserCallScore"];
        DdzVO.getVo().wCurrentUser = data["wCurrentUser"];
        game.AppFacade.instance.sendNotification(PanelNotify.DDZ.START_CALL_SCORE);
    }
    //叫分完成确定庄家
    public callScoreOver(data: Object) {
        DdzVO.getVo().cbTimeOutCard = 30//起手30秒
        DdzVO.getVo().wBankerUser = data["wBankerUser"];
        DdzVO.getVo().cbBankerScore = data["cbBankerScore"];
        DdzVO.getVo().cbBankerCard = data["cbBankerCard"];
        DdzVO.getVo().wCurrentUser = data["wCurrentUser"];
        DdzVO.getVo().cbIsDoubleNumber = DdzVO.getVo().cbBankerScore

        let chessList = Poker_DDZ.idChangeObject(DdzVO.getVo().cbBankerCard);
        DdzVO.getVo().chessListBanker = chessList;
        if (DdzVO.getVo().wBankerUser == DdzVO.getVo().wChairId_my) {
            DdzVO.getVo().cbCardData = DdzVO.getVo().cbCardData.concat(DdzVO.getVo().cbBankerCard);//地主牌ID加入手牌ID
            DdzVO.getVo().chessListMy = DdzVO.getVo().chessListMy.concat(chessList);//地主牌对象加入手牌对象
            DdzVO.getVo().chessListMy = Poker_DDZ.sort(DdzVO.getVo().chessListMy);//手牌对象重新排序刷新页面
        }
        if (DdzVO.getVo().wBankerUser == DdzVO.getVo().wChairId_left) {
            DdzVO.getVo().chessListLeftLength += DdzVO.getVo().cbBankerCard.length;
        }
        if (DdzVO.getVo().wBankerUser == DdzVO.getVo().wChairId_right) {
            DdzVO.getVo().chessListRightLength += DdzVO.getVo().cbBankerCard.length;
        }
        game.AppFacade.instance.sendNotification(PanelNotify.DDZ.END_CALL_SCORE);
    }
    //用户出牌
    public userPlayCards(data: Object) {
        DdzVO.getVo().cbTimeOutCard = 20
        DdzVO.getVo().upPlayList = data["cbCardData"];
        DdzVO.getVo().wCurrentUser = data["wCurrentUser"];
        DdzVO.getVo().wOutCardUser = data["wOutCardUser"];
        game.AppFacade.instance.sendNotification(PanelNotify.DDZ.USER_PLAY_CARD);
    }
    //用户弃牌
    public userGiveUpCards(data: Object) {
        DdzVO.getVo().cbTurnOver = data["cbTurnOver"];
        DdzVO.getVo().wPassCardUser = data["wPassCardUser"];
        DdzVO.getVo().wOutCardUser = data["wOutCardUser"];
        DdzVO.getVo().wCurrentUser = data["wCurrentUser"];
        game.AppFacade.instance.sendNotification(PanelNotify.DDZ.USER_GIVEUP_CARD);
    }
    //游戏结束
    public gameOver(data: Object) {
        DdzVO.getVo().tuoguan = false;
        game.AppFacade.instance.sendNotification(PanelNotify.DDZ.MODIFY_HOSTING_STATUS)
        let cbCardCount: Array<any> = data["cbCardCount"];  //每个用户剩余的牌数量【 数组[2,0,3] 表示0号位玩家剩余2张，1号位玩家剩余0张，2号位玩家剩余三张 】
        let cbHandCardData: Array<any> = data["cbHandCardData"];//所有用户剩余的牌的集合【根据上面数组中的值按顺序取出对应个数的牌，0号位玩家取前2个，1号位玩家不取，2号位玩家取后3个】
        let lGameScore: Array<any> = data["lGameScore"];
        let bChunTian = data["bChunTian"];
        let bFanChunTian = data["bFanChunTian"];

        let chess = {
            0: [],
            1: [],
            2: []
        };
        for (let i = 0; i < cbCardCount.length; i++) {
            let leng = cbCardCount[i];
            let arr = [];
            for (let y = 0; y < leng; y++) {
                arr.push(cbHandCardData[y]);
            }
            chess[i] = arr;
            cbHandCardData.splice(0, leng);
        }
        DdzVO.getVo().bChunTian = bChunTian;
        DdzVO.getVo().bFanChunTian = bFanChunTian;
        DdzVO.getVo().chessListLeftOver = chess[DdzVO.getVo().wChairId_left];
        DdzVO.getVo().chessListRightOver = chess[DdzVO.getVo().wChairId_right];
        //玩家得分
        DdzVO.getVo().score_my = lGameScore[DdzVO.getVo().wChairId_my];
        DdzVO.getVo().score_right = lGameScore[DdzVO.getVo().wChairId_right];
        DdzVO.getVo().score_left = lGameScore[DdzVO.getVo().wChairId_left];

        if (DdzVO.getVo().bChunTian == 1 || DdzVO.getVo().bFanChunTian == 1) {
            DdzVO.getVo().cbIsDoubleNumber *= 2;
        }

        DdzVO.getVo().lUserScore += lGameScore[DdzVO.getVo().wChairId_my];//自己的钱从输赢分中扣除
        game.AppFacade.instance.sendNotification(PanelNotify.DDZ.GAME_OVER);
    }
    public getOutTime(data: Object) {
        var cbTimeOutCard = data["cbTimeOutCard"];
        var cbTimeCallScore = data["cbTimeCallScore"];
        DdzVO.getVo().cbTimeOutCard = cbTimeOutCard
        DdzVO.getVo().cbTimeCallScore = cbTimeCallScore
    }
    //托管
    public setHosting(data: Object) {
        let cbChairID = data["cbChairID"];
        let cbTrustee = data["cbTrustee"];
        if (cbChairID == DdzVO.getVo().wChairId_my) {
            if (cbTrustee == 1) {
                DdzVO.getVo().tuoguan = true;
            } else {
                DdzVO.getVo().tuoguan = false;
            }
            game.AppFacade.instance.sendNotification(PanelNotify.DDZ.MODIFY_HOSTING_STATUS)
        }
    }
    //获取牌局编号
    public getSerialNumber(data: Object) {
        DdzVO.getVo().gameNumbering = data["szSerialNumber"];
        game.AppFacade.instance.sendNotification(PanelNotify.DDZ.GET_SERIAL_NUMBER)
    }
    //重新进游戏数据恢复
    public dataRecovery(data: Object) {
        if (RegDataDdz.MainCMD_MsgId[100][101] == RegDataDdz.jiaofen) {
            this.dataRecoveryByCall(data)
        }
        if (RegDataDdz.MainCMD_MsgId[100][101] == RegDataDdz.gameStart) {
            this.dataRecoveryByPlay(data);
        }
    }
    public dataRecoveryByCall(data: Object) {//叫分阶段数据恢复
        DdzVO.getVo().game_state = 1;
        if (DdzVO.getVo().wChairId_my == 0) {
            DdzVO.getVo().wChairId_right = 1
            DdzVO.getVo().wChairId_left = 2
        }
        if (DdzVO.getVo().wChairId_my == 1) {
            DdzVO.getVo().wChairId_right = 2
            DdzVO.getVo().wChairId_left = 0
        }
        if (DdzVO.getVo().wChairId_my == 2) {
            DdzVO.getVo().wChairId_right = 0
            DdzVO.getVo().wChairId_left = 1
        }
        let cbScoreInfo = data["cbScoreInfo"];//每个人叫分信息 0 不叫 1 1分 2 2分 3 3分 255 没选择
        DdzVO.getVo().jiaofen_my = cbScoreInfo[DdzVO.getVo().wChairId_my]
        DdzVO.getVo().jiaofen_left = cbScoreInfo[DdzVO.getVo().wChairId_left]
        DdzVO.getVo().jiaofen_right = cbScoreInfo[DdzVO.getVo().wChairId_right]

        DdzVO.getVo().cbTimeCallScore = data["cbTimeCallScore"];
        DdzVO.getVo().cbTimeOutCard = data["cbTimeOutCard"];
        DdzVO.getVo().lCellScore = data["lCellScore"];
        DdzVO.getVo().wCurrentUser = data["wCurrentUser"];//等待操作的人(此处有BUG，后台永远返回0，所以即使前端知道每个人叫分的状况也无法判断是否轮到自己按钮该不该亮)
        DdzVO.getVo().cbBankerScore = data["cbBankerScore"];
        DdzVO.getVo().cbCardData = data["cbHandCardData"];

        DdzVO.getVo().chessListMy = Poker_DDZ.idChangeObject(DdzVO.getVo().cbCardData);
        DdzVO.getVo().chessListMy = Poker_DDZ.sort(DdzVO.getVo().chessListMy);
        //记牌器排除我的手牌
        for (let i = 0; i < DdzVO.getVo().cbCardData.length; i++) {
            if (DdzVO.getVo().cbCardData[i] != 0) {
                let poker = Poker_DDZ.chessArray[DdzVO.getVo().cbCardData[i]];
                let value = poker.value;
                if (Global.isNotEmpty(DdzVO.getVo().chessListWhole[value])) {
                    DdzVO.getVo().chessListWhole[value]--;
                }
            }
        }
        game.AppFacade.instance.sendNotification(PanelNotify.DDZ.DATA_RECOVERY_BY_CALL);
    }
    public dataRecoveryByPlay(data: Object) {//打牌阶段数据恢复
        DdzVO.getVo().game_state = 1;
        if (DdzVO.getVo().wChairId_my == 0) {
            DdzVO.getVo().wChairId_right = 1
            DdzVO.getVo().wChairId_left = 2
        }
        if (DdzVO.getVo().wChairId_my == 1) {
            DdzVO.getVo().wChairId_right = 2
            DdzVO.getVo().wChairId_left = 0
        }
        if (DdzVO.getVo().wChairId_my == 2) {
            DdzVO.getVo().wChairId_right = 0
            DdzVO.getVo().wChairId_left = 1
        }

        DdzVO.getVo().wCurrentUser = data["wCurrentUser"];//等待操作的人
        DdzVO.getVo().cbTimeCallScore = data["cbTimeCallScore"];//叫分时间
        DdzVO.getVo().cbTimeOutCard = data["cbTimeOutCard"];//出牌时间
        DdzVO.getVo().lCellScore = data["lCellScore"];//房间底分
        DdzVO.getVo().wBankerUser = data["wBankerUser"];//地主玩家
        DdzVO.getVo().cbBankerCard = data["cbBankerCard"];//地主牌
        DdzVO.getVo().cbBankerScore = data["cbBankerScore"];//地主分
        DdzVO.getVo().cbIsDoubleNumber = DdzVO.getVo().cbBankerScore;//房间倍数（目前有BUG，刷新的时候不知道最新的倍数）
        //上次出牌数据
        var cbTurnCardData: Array<number> = data["cbTurnCardData"];
        DdzVO.getVo().upPlayList = cbTurnCardData.slice(0, data["cbTurnCardCount"]);//上次出牌数组
        DdzVO.getVo().wOutCardUser = data["wTurnUser"]//上次出牌用户

        let cbHandCardCount = data["cbHandCardCount"]//各自手牌数量
        let cbHandCardData: Array<number> = data["cbHandCardData"]//自己的手牌
        DdzVO.getVo().chessListRightLength = cbHandCardCount[DdzVO.getVo().wChairId_right];
        DdzVO.getVo().chessListLeftLength = cbHandCardCount[DdzVO.getVo().wChairId_left];
        DdzVO.getVo().cbCardData = cbHandCardData.slice(0, cbHandCardCount[DdzVO.getVo().wChairId_my]);

        DdzVO.getVo().chessListBanker = Poker_DDZ.idChangeObject(DdzVO.getVo().cbBankerCard);
        DdzVO.getVo().chessListMy = Poker_DDZ.sort(Poker_DDZ.idChangeObject(DdzVO.getVo().cbCardData));
        //记牌器排除我的手牌
        for (let i = 0; i < DdzVO.getVo().cbCardData.length; i++) {
            let poker = Poker_DDZ.chessArray[DdzVO.getVo().cbCardData[i]];
            let value = poker.value;
            if (Global.isNotEmpty(DdzVO.getVo().chessListWhole[value])) {
                DdzVO.getVo().chessListWhole[value]--;
            }
        }
        let cbOutCard: Array<any> = data["cbOutCard"];
        //记牌器排除已经打出的牌
        for (let i = 0; i < cbOutCard.length; i++) {
            if (cbOutCard[i] != 0) {
                let poker = Poker_DDZ.chessArray[cbOutCard[i]];
                let value = poker.value;
                if (Global.isNotEmpty(DdzVO.getVo().chessListWhole[value])) {
                    DdzVO.getVo().chessListWhole[value]--;
                }
            }
        }
        //这里拿到的记牌器数据已经是最新的，但是由于updateRecorder这个方法刷新记牌器图片的时候会再次根据upPlayList 扣除记牌器剩余数量，为避免重复扣除导致数量错误，这里先加回去
        for (let i = 0; i < DdzVO.getVo().upPlayList.length; i++) {
            let poker = Poker_DDZ.chessArray[DdzVO.getVo().upPlayList[i]];
            let value = poker.value;
            if (Global.isNotEmpty(DdzVO.getVo().chessListWhole[value])) {
                DdzVO.getVo().chessListWhole[value]++;
            }
        }
        if (DdzVO.getVo().wOutCardUser == DdzVO.getVo().wChairId_left)
            DdzVO.getVo().chessListLeftLength += DdzVO.getVo().upPlayList.length;
        if (DdzVO.getVo().wOutCardUser == DdzVO.getVo().wChairId_right)
            DdzVO.getVo().chessListRightLength += DdzVO.getVo().upPlayList.length;
        game.AppFacade.instance.sendNotification(PanelNotify.DDZ.DATA_RECOVERY_BY_PLAY);
    }
}
